package com.shou.trigger.http;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shou.api.model.dto.DeleteDTO;
import com.shou.api.model.dto.PageQueryDTO;
import com.shou.api.model.dto.ParkingRecordDTO;
import com.shou.api.model.response.ResultData;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.park.model.ParkingRecordEntity;
import com.shou.domain.park.service.IParkingRecordService;
import com.shou.trigger.http.mapstruct.ParkingRecordDTOConvert;
import com.shou.trigger.http.mapstruct.common.PageQueryDTOConvert;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * ClassName:ParkingRecordController
 * Project:SmartPark
 * Package: com.shou.trigger.http
 * Description
 *
 * @Author Nemo
 * @Create 2025/2/13 21:35
 * @Version 1.0
 */
@Validated
@RestController
@CrossOrigin
@RequestMapping("/api/v1/parking-record")
public class ParkingRecordController extends AbstructController{
    @Resource
    private IParkingRecordService parkingRecordService;
    @Resource
    private PageQueryDTOConvert pageQueryDTOConvert;
    @Resource
    private ParkingRecordDTOConvert parkingRecordDTOConvert;

    /**
     * 分页查询
     */
    @GetMapping("/parking-record-info")
    public ResultData<IPage<ParkingRecordDTO>> page(@RequestParam Map<String, Object> rulePageQueryDTOMap){
        PageQueryDTO pageQueryDTO = PageQueryDTO.getRulePageQueryDTOFromMap(rulePageQueryDTOMap);
        // 入参校验
        validateQueryParams(pageQueryDTO);
        // 将DTO对象转换为实体类
        PageQueryEntity pageQueryEntity = pageQueryDTOConvert.convertPageQueryDTOToEntity(pageQueryDTO);
        // 分页查询
        IPage<ParkingRecordEntity> queueMessageList = parkingRecordService.pageQuery(pageQueryEntity);
        return ResultData.success(parkingRecordDTOConvert.convertParkingRecordEntityPageToDTO(queueMessageList));
    }

    /**
     * 新增停车记录信息
     */
    @PostMapping("/parking-record-info")
    public ResultData<String> insertParkingRecord(@RequestBody ParkingRecordDTO parkingRecordDTO){
        // 将DTO对象转换为Entity对象，调用domain层方法
        ParkingRecordEntity parkingRecordEntity = parkingRecordDTOConvert.convertParkingRecordDTOToEntity(parkingRecordDTO);
        // 保存(新增)规则
        parkingRecordService.insertParkingRecord(parkingRecordEntity);
        return ResultData.success("insertParkingRecord success");
    }

    /**
     * 删除单/多个停车记录信息
     */
    @DeleteMapping("/parking-record-info")
    public ResultData<String> deleteParkingRecordByIds(@RequestBody @Validated DeleteDTO deleteDTO) {
        // 根据传递的ID(可以是多个)删除停车记录信息
        parkingRecordService.deleteByIds(deleteDTO.getIds());
        return ResultData.success("deleteParkingRecordByIds success");
    }

    /**
     * 清空所有停车记录信息
     */
    @DeleteMapping("/all-parking-record-info")
    public ResultData<String> deleteAll() {
        // 清空停车记录信息表
        parkingRecordService.deleteAll();
        return ResultData.success("deleteAll success");
    }

    /**
     * 根据停车记录ID更新停车记录信息
     */
    @PutMapping("/parking-record-info")
    public ResultData<String> updateParkingRecordById(@RequestBody @Validated ParkingRecordDTO rule) {
        // 将DTO对象转换为Entity对象，调用domain层方法
        ParkingRecordEntity parkingRecordEntity = parkingRecordDTOConvert.convertParkingRecordDTOToEntity(rule);
        // 根据ID更新停车记录信息
        parkingRecordService.updateParkingRecordById(parkingRecordEntity);
        return ResultData.success("updateParkingRecordById success");
    }
}
